package com.ruoyi.wms.service;

import com.ruoyi.common.core.utils.MapstructUtils;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.core.utils.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.wms.domain.bo.partolStore.inspectionRecordsBo;
import com.ruoyi.wms.domain.vo.inspectionRecordsVo;
import com.ruoyi.wms.domain.entity.partolStore.inspectionRecords;
import com.ruoyi.wms.mapper.inspectionRecordsMapper;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 巡检记录Service业务层处理
 *
 * @author xhr
 * @date 2024-12-03
 */
@RequiredArgsConstructor
@Service
public class inspectionRecordsService {

    private final inspectionRecordsMapper inspectionRecordsMapper;

    /**
     * 查询巡检记录
     */
    public inspectionRecordsVo queryById(Long id){
        return inspectionRecordsMapper.selectVoById(id);
    }

    /**
     * 查询巡检记录列表
     */
    public TableDataInfo<inspectionRecordsVo> queryPageList(inspectionRecordsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<inspectionRecords> lqw = buildQueryWrapper(bo);
        Page<inspectionRecordsVo> result = inspectionRecordsMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询巡检记录列表
     */
    public List<inspectionRecordsVo> queryList(inspectionRecordsBo bo) {
        LambdaQueryWrapper<inspectionRecords> lqw = buildQueryWrapper(bo);
        return inspectionRecordsMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<inspectionRecords> buildQueryWrapper(inspectionRecordsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<inspectionRecords> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getEquipmentId() != null, inspectionRecords::getEquipmentId, bo.getEquipmentId());
        lqw.between(params.get("beginInspectionTime") != null && params.get("endInspectionTime") != null,
            inspectionRecords::getInspectionTime ,params.get("beginInspectionTime"), params.get("endInspectionTime"));
        lqw.eq(StringUtils.isNotBlank(bo.getInspectors()), inspectionRecords::getInspectors, bo.getInspectors());
        return lqw;
    }

    /**
     * 新增巡检记录
     */
    public void insertByBo(inspectionRecordsBo bo) {
        inspectionRecords add = MapstructUtils.convert(bo, inspectionRecords.class);
        inspectionRecordsMapper.insert(add);
    }

    /**
     * 修改巡检记录
     */
    public void updateByBo(inspectionRecordsBo bo) {
        inspectionRecords update = MapstructUtils.convert(bo, inspectionRecords.class);
        inspectionRecordsMapper.updateById(update);
    }

    /**
     * 批量删除巡检记录
     */
    public void deleteByIds(Collection<Long> ids) {
        inspectionRecordsMapper.deleteBatchIds(ids);
    }
}
